<template>
  <div class="register">
    <div class="register-wrap">
      <div class="switch">
        <router-link to="/login" tag="span">登录</router-link>
        <span>·</span>
        <router-link to="/register" tag="span">注册</router-link>
      </div>

      <validation-observer ref="observer" v-slot="{validate}">
        <div class="input-wrap">
          <validation-provider rules="required|email" name="登录账号" v-slot="{errors}">
            <div class="user-wrap">
              <input
                type="text"
                v-model="username"
                class="username"
                placeholder="登录账号的邮箱"
                autocomplete="off"
              />
              <i class="iconfont iconName">&#xe634;</i>
              <div class="checkNotify">
                <span>{{errors[0]}}</span>
              </div>
            </div>
          </validation-provider>
          <validation-provider rules="required|min:6" name="密码" v-slot="{errors}">
            <div class="user-wrap">
              <input type="text" class="password" v-model="password" placeholder="设置密码" />
              <i class="iconfont iconWord">&#xe622;</i>
              <div class="checkNotify">
                <span>{{errors[0]}}</span>
              </div>
            </div>
          </validation-provider>
          <validation-provider rules="required|min:4" name="昵称" v-slot="{errors}">
            <div class="user-wrap">
              <input
                type="text"
                v-model="realname"
                class="username"
                placeholder="你的昵称"
                autocomplete="off"
              />
              <i class="iconfont iconName">&#xe634;</i>
              <div class="checkNotify">
                <span>{{errors[0]}}</span>
              </div>
            </div>
          </validation-provider>

          <validation-provider rules="required|min:4" name="验证码" v-slot="{errors}">
            <div class="user-wrap code">
              <input type="text" class="password" v-model="code" placeholder="验证码" />
              <span class="svg" v-html="svg" @click="_getCode(sid)"></span>
            </div>
            <div class="checkNotify">
              <span>{{errors[0]}}</span>
            </div>
          </validation-provider>
        </div>
        <div class="btn-register" @click="validate().then(_submitReg)">注册</div>
        <div class="hr"></div>
      </validation-observer>

      <div class="checkInfo">
        <div>点击 “注册” 即表示您同意并愿意遵守知否的</div>
        <div>
          <a href>用户协议</a> 和
          <a href>用户协议</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ValidationProvider, ValidationObserver } from "vee-validate";
import { getCaptcha } from "../common/mixin";
import { submitReg } from "../api/login";
import uuid from "uuid/v4";
export default {
  components: {
    ValidationProvider,
    ValidationObserver
  },
  mixins: [getCaptcha],
  data() {
    return {
      username: "",
      password: "",
      code: "",
      defaultAvatar: "http://img.jj20.com/up/allimg/tx07/0815201914043.jpg",
      svg: "",
      realname: "",
      data: ""
    };
  },
  mounted() {},
  methods: {
    async _submitReg() {
      // 表单验证
      const isValid = await this.$refs.observer.validate();
      if (!isValid) {
        // 表单验证失败
        return;
      }
      submitReg({
        username: this.username,
        password: this.password,
        code: this.code,
        realname: this.realname,
        sid: this.$store.state.sid,
        uhead: this.randomAvatar() //随机头像
      }).then(res => {
        if (res.code == -1) {
          this.$message.warning("用户名已存在!");
          this.username = "";
          return;
        }
        if (res.code == 0) {
          window.localStorage.setItem("token", res.token);
          this.$store.commit('saveUser',res.user)
          this.$message.success(
            `恭喜 ${res.user.UserName}成为本站的第${res.user.UID}名用户,已为您自动登录`
          );
          this.$router.push("/").catch();
        } else {
          this.$message.warning("自动登录失败,请手动登录吧！");
          this.$router.push("/login").catch();
        }
      });
    },
    randomAvatar() {
      let r = Math.floor(43 + Math.random() * (54 + 1 - 43));
      return `http://img.jj20.com/up/allimg/tx07/08152019140${r}.jpg`;
    }
  }
};
</script>

<style lang='scss' scoped>
.register {
  .register-wrap {
    background: white;
    width: 400px;
    margin: 60px auto 0;
    padding: 50px 50px 30px;
    text-align: center;
    box-sizing: border-box;
    box-shadow: 0 0 8px rgba(0, 0, 0, 0.1);
    .switch {
      font-size: 18px;
      span {
        margin: 0 10px;
        display: inline-block;
        cursor: pointer;
        height: 40px;
        line-height: 40px;
      }
    }
    .input-wrap {
      margin-top: 30px;
      input {
        display: block;
        outline: none;
        border: 1px solid #dadada;
        background: #f7f7f7;
        height: 50px;
        width: 100%;
        box-sizing: border-box;
        padding: 5px 0 5px 30px;
        border-radius: 2px;
      }
      input:first-child {
        border-bottom: none;
      }
      .user-wrap {
        position: relative;
      }
      .checkNotify {
        width: 100%;
        height: 25px;
      }
      .code {
        display: flex;
        .svg {
          width: 150px;
          height: 50px;
        }
      }
      .iconfont {
        position: absolute;
        left: 5px;
        top: 15px;
        font-size: 20px;
        color: #969696;
      }
    }
    .code-line {
      display: flex;
      align-items: center;
      .code {
        width: 40%;
        height: 40px;
        padding-left: 5px;
        margin-right: 20px;
        margin-top: 20px;
      }
      .svgImg {
        width: 150px;
        height: 50px;
      }
    }
    .checkInfo {
      margin: 20px 0;
      font-size: 14px;
    }
    .btn-register {
      margin-top: 30px;
      width: 100%;
      height: 40px;
      border-radius: 999px;
      background: #42c02e;
      color: white;
      line-height: 40px;
      font-size: 16px;
      cursor: pointer;
    }
  }
}
</style>